package com.axonactive.yolo.service.implement;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.axonactive.yolo.dao.interfaces.ParamLockerRepository;
import com.axonactive.yolo.domain.ParamLocker;
import com.axonactive.yolo.service.interfaces.ParamLockerService;

@Service
public class ParamLockerServiceImpl implements ParamLockerService {
	
	@Resource
	private ParamLockerRepository paramLockerRepository;
	
	@Override
	public boolean getLockFlag() {
		ParamLocker locker=null;
		//get the first element
		List<ParamLocker> list=paramLockerRepository.findAll();
		if (list.size()==0)
		{
			//add an intance of ParamLocker
			locker=new ParamLocker();
			locker.setParam("ad");
//			locker.setLock(false);
			paramLockerRepository.save(locker);
			return false;
		}
		return list.get(0).isLock();
	}

	@Override
	public boolean setLockFlag(boolean flag) {
		List<ParamLocker> list=paramLockerRepository.findAll();
		ParamLocker locker=null;
		if (list.size()==0)
		{
			locker=new ParamLocker();
			locker.setLock(false);
			locker.setParam("");
			paramLockerRepository.save(locker);
		}
		else
		{
			locker=list.get(0);
			locker.setLock(flag);
			paramLockerRepository.save(locker);
		}
		return flag;
	}

}
